﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Mes.Pendant.ArrivalNotices.Dto;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.NotificationArrivals;
using Mes.Pendant.WorkshopSetups;
using Mes.Pendant.WorkshopSetupService;
using Mes.Pendant.WorkshopSetupService.Dto;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Mes.Pendant.WorkshopSetupServicess
{
	public class WorkshopSetupServices : ApplicationService, IWorkshopSetupServices
    {
        private readonly IRepository<WorkshopSetup, int> repository;
        private readonly IMapper mapper;

        public WorkshopSetupServices(IRepository<WorkshopSetup, int> repository,IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加车间设置信息
        /// </summary>
        /// <param name="insertWorksetupdto"></param>
        /// <returns></returns>
        public async Task<int> AddWorksetup(InsertWorksetupdto insertWorksetupdto)
        {
            var entity = ObjectMapper.Map<InsertWorksetupdto, WorkshopSetup>(insertWorksetupdto);
            await repository.InsertAsync(entity);
            return entity.Id;
        }

        /// <summary>
        /// 获取车间设置数据
        /// </summary>
        /// <param name="workshopCode"></param>
        /// <param name="workshopName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetWorksetups(string? workshopCode, string? workshopName, int index = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<WorkshopSetup>(true);
            if (!string.IsNullOrEmpty(workshopCode))
            {
                predicate=predicate.And(m=>m.WorkshopCode.Contains(workshopCode));
            }
            if (!string.IsNullOrEmpty(workshopName))
            {
                predicate=predicate.And(m=>m.WorkshopName.Contains(workshopName));
            }
            var workList = repository.GetListAsync(predicate).Result;
            PagedResult pagedResult = new PagedResult();
            pagedResult.CurrentPage = index;
            pagedResult.PageSize = limit;
            pagedResult.RowCount=workList.Count();
            pagedResult.Queryable=workList.OrderBy(x=>x.Id).Skip((index-1)*limit).Take(limit).AsQueryable();
            pagedResult.Queryable=pagedResult.Queryable.MapToList<SearchWorksetupdto>().AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 删除车间设置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteWorksetups(int id)
        {
            await repository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 根据id获取车间设置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SelectIdWorkdto SelectByWorkId(int id)
        {
            var entity = repository.GetAsync(id).Result;
            return ObjectMapper.Map<WorkshopSetup, SelectIdWorkdto>(entity);
        }

        /// <summary>
        /// 修改物料型号
        /// </summary>
        /// <param name="selectIdWorkdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateWorksets(SelectIdWorkdto selectIdWorkdto)
        {
            var entity = ObjectMapper.Map<SelectIdWorkdto, WorkshopSetup>(selectIdWorkdto);
            await repository.UpdateAsync(entity);
            return 1;
        }
    }
}
